/*
 * DbJsonConnection.h
 *
 *  Created on: 2018年3月4日
 *      Author: chuanjiang.zh@qq.com
 */

#ifndef DBJSONCONNECTION_H_
#define DBJSONCONNECTION_H_

#include "DbConnectionPool.h"
#include "json/json.h"
#include <vector>
#include <map>


namespace db
{

typedef std::vector< Json::Value >	JsonList;

typedef std::pair< std::string, JsonList >  JsonListGroupItem;
typedef std::vector< JsonListGroupItem >    JsonListGroup;


class DbJsonConnection
{
public:
	static bool rowToJson(db::DbRow& row, Json::Value& json);

	static std::string makeUpdate(const std::string& table, const Json::Value& json);
	static std::string makeInsert(const std::string& table, const std::string& verb, const Json::Value& json);

	static std::string toJsonString(int offset, int limit, int total, JsonList& items);

    static void groupJson(const JsonList& src, const std::string& field, JsonListGroup& jsonGroup);

    static bool listToJson(const JsonList& jsonList, Json::Value& json);
	static bool toJson(const JsonList& jsonList, Json::Value& json);
	static bool toJson(int offset, int limit, int total, const JsonList& items, Json::Value& json);

public:
	explicit DbJsonConnection(DbConnectionPool& pool);
	virtual ~DbJsonConnection();

	bool validate();

	bool query(const std::string& sql, Json::Value& json);
	bool querySome(const std::string& sql, Json::Value& json);

	bool query(const std::string& sql, JsonList& jsonList);

	bool queryToJson(const std::string& sql, std::string& text);
	bool querySomeToJson(const std::string& sql, std::string& text);

	bool execute(const std::string& sql);
	int update(const std::string& sql);

	bool update(const std::string& table, const Json::Value& json, const std::string& condition);

	bool insert(const std::string& sql, const std::string& table, int64_t& id);
	bool insert(const std::string& table, const Json::Value& json);
	bool insert(const std::string& table, const Json::Value& json, int64_t& id);

	bool replace(const std::string& table, const Json::Value& json);

    bool searchTable(const std::string& table, const std::string& query,
        const std::string& condition, const std::string& order,
        int offset, int limit, int& total, JsonList& jsonList);

    bool search(const std::string& countSql, const std::string& query, 
        const std::string& condition, const std::string& order,
			int offset, int limit, int& total, JsonList& jsonList);

	bool count(const std::string& countSql, const std::string& condition, int& total);

	bool pageSearch(const std::string& table, const std::string& idColumn, const std::string& query,
		const std::string& condition, const std::string& order,
		int offset, int limit, JsonList& jsonList);

	bool pageSearch(const std::string& table, const std::string& idColumn, const std::string& countSql, const std::string& query,
		const std::string& condition, const std::string& order,
		int offset, int limit, int& total, JsonList& jsonList);

protected:
	bool count(db::DbConnection& conn, const std::string& countSql, const std::string& condition, int& total);

	bool pageSearch(db::DbConnection& conn, const std::string& table, const std::string& idColumn, const std::string& query,
		const std::string& condition, const std::string& order,
		int offset, int limit, JsonList& jsonList);

protected:
	DbConnectionPool& m_pool;
};


} /* namespace db */

#endif /* DBJSONCONNECTION_H_ */
